package com.sf.dlb.mapper.dao;

import com.sf.dlb.jpa.model.NewRecodInfo;
import com.sf.dlb.mapper.entity.NewRecodListEntity;
import com.sf.dlb.mapper.entity.UserFriendListEntity;
import com.sf.dlb.mapper.entity.UserReaderNewClassEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.mapstruct.Mapper;


import java.util.List;


/**
 * 用户阅读列表 Repository
 *
 * Created by jing.tan on 2018/5/21.
 */
@Mapper
public interface NewRecodMapper {
    /**
     * 查询用户阅读行为数据-统计
     * @param userId
     * @return
     */
    @Select("select a.user_id as \"userId\",DATE_FORMAT(a.create_date,'%Y-%m-%d') as \"everyDayDate\",count(*) as \"everyDayReadNumer\",sum(a.get_number) as \"everyDayDlb\",sum(a.read_time_long) as \"everyDayReadTime\",group_concat(a.class_id) as \"newClass\" from new_recod a where a.user_id=#{userId} and a.del_flag=#{delFlag} AND a.get_state=#{getState} group by DATE_FORMAT(a.create_date,'%Y-%m-%d') order by everyDayDate desc")
    List<NewRecodListEntity> findByUserReaderBehaviourStatistic(@Param("userId") String userId, @Param("delFlag")String delFlag, @Param("getState")String getState);
    /**
     * 查询用户下级好友收益-统计
     * @param userId
     * @return
     */
    @Select("select a.invitation_user_id,u.name,u.mobile,u.create_date,n.read_time_long,n.read_count,n.history_getdlb_count,IFNULL(w.lock_dlb,0)+IFNULL(w.unlock_dlb,0)+IFNULL(w.available_dlb,0) as 'walletCount' FROM `user_invitation` a LEFT JOIN user_info u on a.invitation_user_id=u.user_id LEFT JOIN user_new_info n ON a.invitation_user_id=n.user_id LEFT JOIN user_wallet w ON a.invitation_user_id=w.user_id where a.user_id=#{userId} order by a.create_date desc")
    List<UserFriendListEntity> findByUserFriendBalance(@Param("userId") String userId);

    /**
     * 查询用户每个分类阅读时长阅读数量-统计
     * @param userId
     * @return
     */
    @Select("SELECT a.id as \"Id\",a.class_id as \"classId\",(select c.name from new_class c where c.class_id=a.class_id) as \"newClassName\", count(*) as \"readerNumber\",sum(read_time_long) as \"readerTime\" FROM new_recod a WHERE DATE_FORMAT(a.create_date, '%Y-%m-%d') = #{createDate} AND a.user_id = #{userId} AND a.del_flag=#{delFlag} and a.get_state=#{getState} GROUP BY class_id")
    List<UserReaderNewClassEntity> findByUserReaderNewClassStatistic(@Param("userId") String userId, @Param("createDate") String createDate, @Param("delFlag") String delFlag,@Param("getState")String getState);
    /**
     * 查询用户阅读-userId,状态,日期
     * @param userId
     * @return
     */
    @Select("select * from new_recod a where a.user_id=#{userId} and a.get_state=#{getState} and DATE_FORMAT(a.create_date,'%Y-%m-%d')=#{createDate}")
    List<NewRecodInfo> findByUserIdAndGetStateAndCreateDate(@Param("userId") String userId, @Param("getState") String getState, @Param("createDate") String createDate);
}